package org.kjtc.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.kjtc.entity.DeviceObject;
import org.kjtc.entity.User;
import org.kjtc.entity.UserInfo;
import org.kjtc.service.UserInfoService;
import org.kjtc.util.Const;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SystemInfoContorller extends BaseController {

	@Resource(name = "deviceObject")
    private DeviceObject deviceObject;

	@Resource(name = "userInfoService")
	private UserInfoService userInfoService;

    @RequestMapping(value = "/getMenuData")
    @ResponseBody
    public Map<String, Object> getMenuData(HttpServletRequest request){

    	Map<String, Object> map = new HashMap<String, Object>();
		Date date = new Date();
		map.put("date", date);
		if (this.getUesr() != null) {
			User user = this.getUesr();
			if (user.getUserID() == null || user.getUserID().isEmpty()) {
				map.put("userName", "未知");
			} else {
				try {
					if (this.getSession().getAttribute(Const.SESSION_USERINFO) == null) {
						UserInfo userInfo = new UserInfo();
						userInfo.setUserID(user.getUserID());
						List<UserInfo> userInfoList = userInfoService.getUserInfo(userInfo);
						if (userInfoList.size() == 1) {
							this.getSession().setAttribute(Const.SESSION_USERINFO, userInfoList.get(0));
							map.put("userName", this.getUesrInfo().getUserName());
							map.put("level", this.getUesrInfo().getLevel());
							map.put("branchCompanyID", this.getUesrInfo().getBranchCompanyID());
							if(userInfoList.get(0).getRoleID().equals("LR_USER_SHRO")) {
								List<UserInfo> list = userInfoService.getUserTeam(userInfo);
								if(list.size() == 1) {
									map.put("subBranchCompanyID", list.get(0).getSubBranchCompanyID());
									map.put("teamID", list.get(0).getTeamID());
								}
							}
						} else {
							map.put("userName", "未知");
						}
						userInfo = null;
					} else {
						map.put("userName", this.getUesrInfo().getUserName());
						map.put("level", this.getUesrInfo().getLevel());
						map.put("branchCompanyID", this.getUesrInfo().getBranchCompanyID());
						map.put("subBranchCompanyID", this.getUesrInfo().getSubBranchCompanyID());
						map.put("teamID", this.getUesrInfo().getTeamID());
					}
				} catch (Exception e) {
					e.printStackTrace();
					map.put("userName", "未知");
					logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "获取系统数据异常：" + e.getMessage());
				}
			}
		} else {
			map.put("userName", "未知");
		}
		date = null;
    	return map;
    }
}